export class H5Ad {

    public static get inst(): any {
        //@ts-ignore
        return window.h5api
    }

    public static init(): void {
        console.log('h5ad init')
    }

    public static canPlayAd(): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            if (!this.inst) {
                reject(false)
            } else {
                this.inst.canPlayAd((data) => {
                    if (data.canPlayAd) {
                        resolve()
                    } else {
                        reject()
                    }
                    console.log("是否可播放广告", data.canPlayAd, "剩余次数", data.remain)
                })
            }
        })
    }

    public static showRewardedVideo(onSuccess: Function, onFail?: Function, onError?: Function): void {
        if (!this.inst) return
        this.inst.playAd((obj) => {
            console.log("代码:" + obj.code + ",消息:" + obj.message);
            if (obj.code === 10000) {
                console.log("开始播放");
            } else if (obj.code === 10001) {
                console.log("播放结束");
                onSuccess && onSuccess()
            } else {
                console.log("广告异常");
                onError && onError()
            }
        })
    }

}

